//
//  LoadingVC.m
//  xychain_DM_iOS
//
//  Created by Jpwang on 2018/5/26.
//  Copyright © 2018年 Jpwang. All rights reserved.
//

#import "LoadingVC.h"
#import "DeviceBindGuideVC.h"

@interface LoadingVC ()

@end

@implementation LoadingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.viewControllers = [NSArray arrayWithObject:self.navigationController.viewControllers.lastObject];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self initView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)initView {
    id isFirst = getValueByKey(@"isFirst");
    if (!isFirst) {
        saveValueByKey([NSNumber numberWithBool:NO], @"isFirst");
        UIViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserGuideVCID"];
        [self.navigationController pushViewController:vc animated:NO];
        return;
    }
    [self retry];
}

- (void)retry {
    if ([[UserManager shareInstance] isUserLogin]) {
        [[[HttpRequest shareInstance] signGet:d_device_list map:nil] subscribeNext:^(id x) {
            if ([x[@"code"] intValue] == 200) {
                NSError *error;
                NSArray *tempArr = [MTLJSONAdapter modelsOfClass:[DeviceItem class] fromJSONArray:x[@"data"] error:&error];
                if (error) {
                    [JPToast showText:error.domain];
                    [self retry];
                } else {
                    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    if (tempArr.count>=0) {
                        UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"TabBarControllerID"];
                        [self.navigationController pushViewController:vc animated:NO];
                    } else {                        
                        NSString *msg = @"当前账号还没有绑定设备";
                        UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
                        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"切换账号" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                            [[UserManager shareInstance] logout];
                            UIViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserLoginVCID"];
                            [self.navigationController pushViewController:vc animated:NO];
                        }];
                        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"绑定设备" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                            DeviceBindGuideVC *vc = [storyboard instantiateViewControllerWithIdentifier:@"DeviceBindGuideVCID"];
                            [vc setIsFromLoadingVC:YES];
                            [self.navigationController pushViewController:vc animated:NO];
                        }];
                        [vc addAction:cancel];
                        [vc addAction:confirm];
                        [self presentViewController:vc animated:YES completion:nil];
                    }
                }
            } else {
                NSString *msg = x[@"message"];
                UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                    exit(0);
                }];
                UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"重试" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    [self retry];
                }];
                [vc addAction:cancel];
                [vc addAction:confirm];
                [self presentViewController:vc animated:YES completion:nil];
            }
        } error:^(NSError *error) {
            NSString *msg = error.localizedDescription;
            UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                exit(0);
            }];
            UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                [self retry];
            }];
            [vc addAction:cancel];
            [vc addAction:confirm];
            [self presentViewController:vc animated:YES completion:nil];
        }];
    } else {
        UIViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil] instantiateViewControllerWithIdentifier:@"UserLoginVCID"];
        [self.navigationController pushViewController:vc animated:NO];
    }
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}


@end
